package cn.iocoder.yudao.module.dlbz.controller.admin.companyscorerecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dlbz.controller.admin.companyscorerecord.vo.*;
import cn.iocoder.yudao.module.dlbz.dal.dataobject.companyscorerecord.CompanyScoreRecordDO;
import cn.iocoder.yudao.module.dlbz.service.companyscorerecord.CompanyScoreRecordService;

@Tag(name = "管理后台 - 企业积分分配流水")
@RestController
@RequestMapping("/dlbz/company-score-record")
@Validated
public class CompanyScoreRecordController {

    @Resource
    private CompanyScoreRecordService companyScoreRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建企业积分分配流水")
    @PreAuthorize("@ss.hasPermission('dlbz:company-score-record:create')")
    public CommonResult<Long> createCompanyScoreRecord(@Valid @RequestBody CompanyScoreRecordSaveReqVO createReqVO) {
        return success(companyScoreRecordService.createCompanyScoreRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新企业积分分配流水")
    @PreAuthorize("@ss.hasPermission('dlbz:company-score-record:update')")
    public CommonResult<Boolean> updateCompanyScoreRecord(@Valid @RequestBody CompanyScoreRecordSaveReqVO updateReqVO) {
        companyScoreRecordService.updateCompanyScoreRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除企业积分分配流水")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dlbz:company-score-record:delete')")
    public CommonResult<Boolean> deleteCompanyScoreRecord(@RequestParam("id") Long id) {
        companyScoreRecordService.deleteCompanyScoreRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得企业积分分配流水")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dlbz:company-score-record:query')")
    public CommonResult<CompanyScoreRecordRespVO> getCompanyScoreRecord(@RequestParam("id") Long id) {
        CompanyScoreRecordDO companyScoreRecord = companyScoreRecordService.getCompanyScoreRecord(id);
        return success(BeanUtils.toBean(companyScoreRecord, CompanyScoreRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得企业积分分配流水分页")
    @PreAuthorize("@ss.hasPermission('dlbz:company-score-record:query')")
    public CommonResult<PageResult<CompanyScoreRecordRespVO>> getCompanyScoreRecordPage(@Valid CompanyScoreRecordPageReqVO pageReqVO) {
        PageResult<CompanyScoreRecordDO> pageResult = companyScoreRecordService.getCompanyScoreRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CompanyScoreRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出企业积分分配流水 Excel")
    @PreAuthorize("@ss.hasPermission('dlbz:company-score-record:export')")
    @OperateLog(type = EXPORT)
    public void exportCompanyScoreRecordExcel(@Valid CompanyScoreRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CompanyScoreRecordDO> list = companyScoreRecordService.getCompanyScoreRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "企业积分分配流水.xls", "数据", CompanyScoreRecordRespVO.class,
                        BeanUtils.toBean(list, CompanyScoreRecordRespVO.class));
    }

}